package com.hiflying.smartlink.v8;

import android.text.TextUtils;

import org.java_websocket.drafts.Draft_75;

public class AirkissProtocol {
    private byte[] bDataSend;
    private byte[] bMagic;
    private byte[] bPrefix;
    private int offset = 0;

    public AirkissProtocol(String ssid, String password, String others) {
        init(ssid, password, others);
    }

    public int getOffset() {
        return this.offset;
    }

    public void setOffset(int offset2) {
        this.offset = offset2;
    }

    public int[] getByte(int flyTimes) {
        if (flyTimes < 40) {
            return new int[]{1, 2, 3, 4};
        }
        if (flyTimes < 60) {
            return new int[]{(this.bMagic[0] & Draft_75.END_OF_FRAME) + this.offset, (this.bMagic[1] & Draft_75.END_OF_FRAME) + this.offset, (this.bMagic[2] & Draft_75.END_OF_FRAME) + this.offset, (this.bMagic[3] & Draft_75.END_OF_FRAME) + this.offset};
        }
        if (flyTimes < 80) {
            return new int[]{(this.bPrefix[0] & Draft_75.END_OF_FRAME) + this.offset, (this.bPrefix[1] & Draft_75.END_OF_FRAME) + this.offset, (this.bPrefix[2] & Draft_75.END_OF_FRAME) + this.offset, (this.bPrefix[3] & Draft_75.END_OF_FRAME) + this.offset};
        }
        int blocks = this.bDataSend.length / 6;
        if (blocks * 6 < this.bDataSend.length) {
            blocks++;
        }
        if ((((flyTimes - 40) - 20) - 20) / blocks >= 15) {
            return null;
        }
        int pos = ((((flyTimes - 40) - 20) - 20) % blocks) * 6;
        int len = 6;
        if (pos + 6 > this.bDataSend.length) {
            len = this.bDataSend.length - pos;
        }
        int[] ret = new int[len];
        ret[0] = (this.bDataSend[pos] & Draft_75.END_OF_FRAME) + this.offset;
        ret[1] = (this.bDataSend[pos + 1] & Draft_75.END_OF_FRAME) + this.offset;
        for (int i = 2; i < len; i++) {
            ret[i] = (this.bDataSend[pos + i] & Draft_75.END_OF_FRAME) + 256 + this.offset;
        }
        return ret;
    }

    private void init(String ssid, String pwd, String usrInfo) {
        int bPwdUsrLen;
        byte[] bssid = ssid.getBytes();
        byte[] bpwd = pwd.getBytes();
        byte[] busrInfo = TextUtils.isEmpty(usrInfo) ? new byte[0] : usrInfo.getBytes();
        int bPwdUsrLen2 = bpwd.length;
        if (busrInfo.length > 0) {
            bPwdUsrLen = bPwdUsrLen2 + busrInfo.length + 1;
        } else {
            bPwdUsrLen = bPwdUsrLen2 + 1;
        }
        byte[] bPwdUsr = new byte[bPwdUsrLen];
        System.arraycopy(bpwd, 0, bPwdUsr, 0, bpwd.length);
        int pos = 0 + bpwd.length;
        int pos2 = pos + 1;
        bPwdUsr[pos] = 27;
        if (busrInfo.length > 0) {
            System.arraycopy(busrInfo, 0, bPwdUsr, pos2, busrInfo.length);
        }
        int dLen = bPwdUsr.length + 1 + bssid.length;
        byte[] bData = new byte[dLen];
        System.arraycopy(bPwdUsr, 0, bData, 0, bPwdUsr.length);
        int pos3 = 0 + bPwdUsr.length;
        bData[pos3] = -86;
        System.arraycopy(bssid, 0, bData, pos3 + 1, bssid.length);
        this.bMagic = getMagicBytes(dLen, crc8(bssid, 0, bssid.length));
        byte[] bPLen = itob(bPwdUsr.length);
        this.bPrefix = getPrefixBytes(bPwdUsr.length, crc8(bPLen, 0, bPLen.length));
        int dblocks = bData.length / 4;
        if (dblocks * 4 < bData.length) {
            dblocks++;
        }
        this.bDataSend = new byte[((dblocks * 2) + bData.length)];
        int pos4 = 0;
        for (int i = 0; i < dblocks; i++) {
            byte[] block = getBlockBytes(bData, i * 4);
            System.arraycopy(block, 0, this.bDataSend, pos4, block.length);
            pos4 += block.length;
        }
    }

    private byte[] getBlockBytes(byte[] data, int pos) {
        int len = data.length - pos;
        if (len > 4) {
            len = 4;
        }
        byte[] b = new byte[(len + 2)];
        b[1] = (byte) (pos / 4);
        System.arraycopy(data, pos, b, 2, len);
//        b[0] = (byte) ((crc8(b, 1, len + 1) & TransportMediator.KEYCODE_MEDIA_PAUSE) | 128);
        b[0] = (byte) ((crc8(b, 1, len + 1) ) | 128);
        b[1] = (byte) (b[1] | 128);
        return b;
    }

    private byte[] getMagicBytes(int dLen, int ssidCrc) {
        return new byte[]{(byte) ((dLen >> 4) & 15), (byte) (((dLen >> 0) & 15) | 16), (byte) (((ssidCrc >> 4) & 15) | 32), (byte) (((ssidCrc >> 0) & 15) | 48)};
    }

    private byte[] getPrefixBytes(int pLen, int plCrc) {
        return new byte[]{(byte) (((pLen >> 4) & 15) | 64), (byte) (((pLen >> 0) & 15) | 80), (byte) (((plCrc >> 4) & 15) | 96), (byte) (((plCrc >> 0) & 15) | 112)};
    }

    private byte[] itob(int i) {
        int bLen;
        if (i > 16777215) {
            bLen = 4;
        } else if (i > 65535) {
            bLen = 3;
        } else if (i > 255) {
            bLen = 2;
        } else {
            bLen = 1;
        }
        byte[] b = new byte[bLen];
        for (int n = 0; n < bLen; n++) {
            b[n] = (byte) ((i >> (n * 8)) & 255);
        }
        return b;
    }

    private int crc8(byte[] bytes, int pos, int len) {
        int i;
        int crc = 0;
        while (len > 0) {
            crc = (crc ^ (bytes[pos] & Draft_75.END_OF_FRAME)) & 255;
            for (int i2 = 0; i2 < 8; i2++) {
                if ((crc & 1) != 0) {
                    i = (crc >> 1) ^ 140;
                } else {
                    i = crc >> 1;
                }
                crc = i & 255;
            }
            pos++;
            len--;
        }
        return crc;
    }
}
